function GetXmlHttpObject()
{
	var xmlHttp = null;
	try
	{
	// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	}
	catch (e)
	{
	// Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

var xmlHttp;
var strAddedName;
var strSelector;

function ajaxAddItem(selector, table, field, message)
{
	//check for correct function input
	if(!selector || !table || !field || !message) 
		return;

	//Get input value
	var answer = prompt (message, "");
	if (answer == null || answer.trim() == "")
		return;
	
	strAddedName = answer;
	strSelector = selector;
	
	xmlHttp = GetXmlHttpObject();
	if (xmlHttp == null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	}
			
	var url = table + "?mode=quickInsert&" + field + "=" + encodeURIComponent(strAddedName);
	xmlHttp.onreadystatechange = stateAddItemChanged;
	
	xmlHttp.open("GET", url, true);
	xmlHttp.setRequestHeader("Content-Type", "text/xml;charset=UTF-8");
	xmlHttp.send(null);
}

function stateAddItemChanged()
{
	if (xmlHttp.readyState == 4)
	{
		var xmlDoc = xmlHttp.responseXML.documentElement; 
		var abc = xmlDoc.getElementsByTagName("id");
		var strId = xmlDoc.getElementsByTagName("id")[0].childNodes[0].nodeValue;
		
		$(strSelector).append( 
				$('<option selected/>').val(strId).html(strAddedName) 
			); 
		$.hyjack_select.update(strSelector);
		
	}
}

function ajaxAddItemTwoSelectors(selector1, selector2,table, field, message)
{
	//check for correct function input
	if(!selector1 || !table || !field || !message) 
		return;

	//Get input value
	var answer = prompt (message, "");
	if (answer == null || answer.trim() == "")
		return;
	
	strAddedName = answer;
	strSelector1 = selector1;
	strSelector2 = selector2;
	
	xmlHttp = GetXmlHttpObject();
	if (xmlHttp == null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	}
			
	var url = table + "?mode=quickInsert&" + field + "=" + encodeURIComponent(strAddedName);
	xmlHttp.onreadystatechange = stateAddItemChangedTwoSelectors;
	
	xmlHttp.open("GET", url, true);
	xmlHttp.setRequestHeader("Content-Type", "text/xml;charset=UTF-8");
	xmlHttp.send(null);
}

function stateAddItemChangedTwoSelectors()
{
	if (xmlHttp.readyState == 4)
	{
		var xmlDoc = xmlHttp.responseXML.documentElement; 
		var abc = xmlDoc.getElementsByTagName("id");
		var strId = xmlDoc.getElementsByTagName("id")[0].childNodes[0].nodeValue;
		
		$(strSelector1).append( 
				$('<option selected/>').val(strId).html(strAddedName) 
			); 
		$.hyjack_select.update(strSelector1);
		
		
		$(strSelector2).append( 
				$('<option selected/>').val(strId).html(strAddedName) 
			); 
		$.hyjack_select.update(strSelector2);
	}
}
